package cn.edu.ujn.booksshop.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.ujn.booksshop.dao.Author;
import cn.edu.ujn.booksshop.dao.Stock;
import cn.edu.ujn.booksshop.dao.User;
import cn.edu.ujn.booksshop.service.IStockService;
import cn.edu.ujn.util.Page;

@Controller
public class StockController {

	@Autowired
	private IStockService stockService;
	
	@GetMapping("/stocks/list.action")
	public String list(Model model,@RequestParam(defaultValue="1")Integer page , @RequestParam(defaultValue ="10")Integer rows,String bookName )
	{
		System.out.println(bookName);
		Page<Stock> selectAll = this.stockService.selectAll(page, rows);
		model.addAttribute("page", selectAll);
//		System.out.println(authorName);
		return "stocks" ;
	}
	
	@RequestMapping("/stock/create.action")
	@ResponseBody
	public String stockCreat(Stock stock,HttpSession session ) {
		User user = (User) session.getAttribute("USER_SESSION");
		stock.setIsdeleted(false);
//		System.out.println(stock.getBookid());
//		System.out.println(stock.getQuantity());
//		System.out.println(stock.getIsdeleted());
	int rows=stockService.insert(stock);
	if(rows > 0){
        return "OK";
    }else{
        return "FAIL";
    }		
	}
	
	@RequestMapping("/stock/getStockById.action")
	@ResponseBody
	public Stock getStockById(Integer id) {
		Stock stock = stockService.selectByPrimaryKey(id);
//		System.out.println(stock.getBookid());
//		System.out.println(stock.getStockid());
//		System.out.println(stock.getQuantity());
	    return stock;
	}

	@RequestMapping("/stock/update.action")
	@ResponseBody
	public String stockUpdate(Stock stock) {
//		System.out.println(stock.getStockid());
//		System.out.println(stock.getBookid());
//	
//		System.out.println(stock.getQuantity());
		
	    int rows = stockService.updateByPrimaryKey(stock);
	    if(rows > 0){
	        return "OK";
	    }else{
	        return "FAIL";
	    }
//		return "OK";
	}
	
	@RequestMapping("/stock/delete.action")
	@ResponseBody
	public String stockDelete(Integer id) {
	    int rows = stockService.deleteByPrimaryKey(id);
	    if(rows > 0){			
	        return "OK";
	    }else{
	        return "FAIL";			
	    }
	}
}
